<template>
	<div id="section">
		<index>
			<el-breadcrumb separator="/">
				<el-breadcrumb-item>管理</el-breadcrumb-item>
				<el-breadcrumb-item><a style="color: #000000;">部门管理</a></el-breadcrumb-item>
			</el-breadcrumb>
			<div class="content">
				<div class="u-f">
					<div class="left">
						<div class="add u-f-justify el-icon-plus" @click="openDialog(1)"></div>
						<div class="building-list">
							<div v-for="(items, index) in sectionList" :label="items.name" :key="index">
								<div class="u-f-item item u-f-jsb" style="position: relative;" @click="leftTap(index,items.id)" :class="index == leftIndex ? 'active' : ''" @mouseover="items.isbtn = true"
										@mouseleave="items.isbtn = false">
									<div class="u-f-item">
										<div style="width: 20px;">
											<i
												v-if="items.children.length"
												@click.stop="items.isShow = !items.isShow"
												:class="items.isShow ? 'el-icon-caret-bottom' : 'el-icon-caret-right'"
											></i>
										</div>
										<div style="cursor: pointer;">{{ items.name }}</div>
									</div>
									<div class="u-f-item btn" v-if="items.isbtn" style="position: absolute;right: 10px;">
										<i class="el-icon-plus u-f-justify" @click.prevent @click.stop="openDialog(1, items)"></i>
										<i class="el-icon-edit-outline u-f-justify" @click.prevent @click.stop="openDialog(2, items)"></i>
										<i class="el-icon-delete u-f-justify" @click.prevent @click.stop="delPlace(items.id)"></i>
									</div>
								</div>
								<div class="floor-list" v-if="items.children.length && items.isShow">
									<div
										class="floor-item u-f-item u-f-jsb"
										@mouseover="item.isbtn = true"
										@mouseleave="item.isbtn = false"
										:class="checkedindex == i ? 'active' : ''"
										v-for="(item, i) in items.children"
										:key="i"
										@click="leftTap1(item.id,i)"
										 style="position: relative;"
									>
										<div>{{ item.name }}</div>
										<div class="u-f-item btn" v-if="item.isbtn" style="position: absolute;right: 10px;">
											<i class="el-icon-edit-outline u-f-justify" @click.prevent @click.stop="openDialog(2, item)"></i>
											<i class="el-icon-delete u-f-justify" @click.prevent @click.stop="delPlace(item.id)"></i>
										</div>
									</div>
								</div>
							</div>
						</div>
					</div>
					<div class=" right">
						<div class="title u-f-item u-f-jsb">
							<div>共{{ total }}条数据</div>
						</div>
						<el-table :data="peopleList" height="93%" ref="multipleTable" tooltip-effect="dark" style="width: 100%;border: 1px solid #EEEEEE;">
							<!-- <el-table-column type="selection" width="55" align="center"></el-table-column> -->
							<el-table-column label="序号" type="index" width="65" align="center"></el-table-column>
							<el-table-column prop="username" label="教师" align="center"></el-table-column>
							<el-table-column label="性别" align="center">
								<template slot-scope="scope">
									<span v-if="scope.row.gender == 1">男</span>
									<span v-if="scope.row.gender == 2">女</span>
									<span v-if="scope.row.gender == 3">未知</span>
								</template>
							</el-table-column>
							<el-table-column prop="department_name" label="部门负责人" align="center">
								<template slot-scope="scope">
									<el-switch :value ="scope.row.department_lead=='1'?true:false" @change="shezhi(scope.row)"></el-switch>
								</template>
							</el-table-column>
						</el-table>
						<div class="u-f-right" style="padding: 15px 0;">
							<!-- <el-button type="danger" plain size="mini" @click="delUser">删除</el-button> -->
							<el-pagination
								@size-change="handleSizeChange"
								@current-change="handleCurrentChange"
								:current-page.sync="page"
								:page-size="limit"
								layout="total, prev, pager, next"
								:total="total"
							></el-pagination>
						</div>
					</div>
				</div>
			</div>
			

			<!-- 编辑 -->
			<el-dialog title="编辑部门" :visible.sync="editVisible" width="500px" :before-close="handleClose">
				<div class="edit-box">
					<div class="u-f-item input-box">
						<div class="input-lebal">
							<span>*</span>
							部门名称
						</div>
						<el-input v-model="name" placeholder="请输入名称" style="width: 70%;"></el-input>
					</div>
				</div>
				<span slot="footer" class="dialog-footer u-f-item u-f-jsb">
					<el-button @click="editVisible = false" size="small">取 消</el-button>
					<el-button type="primary" size="small" @click="submit(2)">确 定</el-button>
				</span>
			</el-dialog>
			<!-- 添加 -->
			<el-dialog title="添加部门" :visible.sync="addVisible" width="500px" :before-close="handleClose">
				<div class="edit-box">
					<div class="u-f-item input-box">
						<div class="input-lebal">
							<span>*</span>
							部门名称
						</div>
						<el-input v-model="name" placeholder="请输入名称" style="width: 70%;"></el-input>
					</div>
				</div>
				<span slot="footer" class="dialog-footer u-f-item u-f-jsb">
					<el-button @click="addVisible = false" size="small">取 消</el-button>
					<el-button type="primary" size="small" @click="submit(1)">确 定</el-button>
				</span>
			</el-dialog>
		</index>
	</div>
</template>

<script>
import index from '@/components/common/index.vue';
export default {
	components: {
		index
	},
	data() {
		return {
			upPad: false,
			mobile: '',
			newpassword: '',
			leftIndex: 0,
			condition: '',
			conditionList: [],
			sousuo: '',
			sectionList: [],
			editVisible: false,
			addVisible: false,
			total: 0,
			page: 1,
			limit: 13,
			name: '',
			pid: '',
			school_place_room_id: [],
			roomList: [],
			id: '',
			ids: [],
			userid: '',
			name: '',
			birthday: '',
			page: 1,
			sousuo: '',
			sousuo1: '',
			addVisible1: false,
			VisibleTitle: '添加人员',
			powerVisible: false,
			sex: 1,
			is_discipline: 1,
			is_department: 1,
			identity_type: 1,
			staff_type: 1,
			phone: '',
			identity: '',
			email: '',
			QQ: '',
			group_id: '',
			school_discipline_id: [],
			school_department_id: [],
			subjectList: [],
			sectionList: [],
			peopleList: [],
			roleList: [],
			type: 1,
			importVisible: false,
			file: '',
			checkedindex:-1,
			department_id:''
		};
	},
	mounted() {
		this.getSectionList();
		this.getroleList();
	},
	methods: {
		leftTap(index,id) {
			this.leftIndex = index;
			this.checkedindex = -1;
			this.department_id = id;
			this.page = 1;
			this.userList();
		},
		leftTap1(id,index){
			this.leftIndex = -1;
			this.checkedindex = index;
			this.department_id = id;
			this.page = 1;
			this.userList();
		},
		// 获取角色列表
		getroleList() {
			this.$api.pepople.roleList().then(res => {
				if (res.data.code == 1) {
					let list = res.data.data.rows;
					this.roleList = list;
				}
			});
		},

		// 初始化表单
		initialize() {
			this.name = '';
			this.pid = '';
		},
		// 关
		shezhi(item){
			console.log(item)
			this.$api.setting.Userlead({
				department_id:item.department_id,
				user_id:item.id,
				type:item.department_lead=='1'?0:1
			}).then(res=>{
				if(res.data.code==1){
					this.$message.success('设置成功');
					this.userList()
				}else{
					this.$message.error(res.data.msg);
				}
			})
		},
		// 人员列表
		userList() {
			let data = {
				limit: this.limit,
				page: this.page
			};
			if (this.department_id) {
				data.department_id = this.department_id;
			}
			this.$api.setting.users(data).then(res => {
				if (res.data.code == 1) {
					this.peopleList = res.data.data.rows;
					this.total = res.data.data.total;
				}
			});
		},
		// 关闭弹窗
		handleClose(done) {
			this.editVisible = false;
			this.addVisible = false;
			this.addVisible1 = false;
			this.initialize();
			done();
		},
		handleSelectionChange(val) {
			let arr = [];
			for (let i in val) {
				arr.push(val[i].id);
			}
			this.ids = arr;
			console.log(this.ids);
		},
		handleSizeChange(val) {
			console.log(`每页 ${val} 条`);
		},
		handleCurrentChange(val) {
			this.page = val;
			this.userList();
		},
		openDialog(type, item) {
			if (type == 1) {
				this.addVisible = true;
				if (item) {
					this.pid = item.id;
				} else {
					this.pid = 0;
				}
				console.log(item);
			} else {
				this.editVisible = true;
				this.id = item.id;
				this.name = item.name;
				this.pid = item.pid;
			}
		},
		// 获取部门列表
		getSectionList() {
			this.$api.setting.sectionList({}).then(res => {
				if (res.data.code == 1) {
					let data = res.data.data;
					for (let i in data) {
						data[i].isShow = false;
						data[i].isbtn = false;
						for (let j in data[i].children) {
							data[i].children[j].checked = false;
							data[i].children[j].isbtn = false;
						}
					}
					this.sectionList = data;
					this.department_id = this.sectionList[0].id;
					this.userList();
				}
			});
		},
		submit(type) {
			let api = this.$api.setting.sectionAdd;
			let data = {
				name: this.name,
				pid: this.pid || 0
			};
			if (type == 2) {
				data.id = this.id;
				api = this.$api.setting.sectionEdit;
			}
			api(data).then(res => {
				if (res.data.code == 1) {
					if (type == 1) {
						this.$message.success('添加成功');
					} else {
						this.$message.success('修改成功');
					}
					this.addVisible = false;
					this.editVisible = false;
					this.initialize();
					this.getSectionList();
				} else {
					this.$message.error(res.data.msg);
				}
			});
		},
		delPlace(id) {
			let _this = this;
			_this
				.$confirm('确定要删除？', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消'
				})
				.then(() => {
					_this.$api.setting
						.sectionDel({
							id: id
						})
						.then(res => {
							if (res.data.code == 1) {
								_this.$message.success('删除成功');
								_this.getSectionList();
							} else {
								_this.$message.error(res.data.msg);
							}
						});
				});
		}
	}
};
</script>

<style lang="scss">
#section {
	.head {
		background-color: #ffffff;
		padding: 10px 30px;
		margin-top: 15px;
		.span {
			margin-right: 10px;
			background-color: #f9f9f9;
			color: #8d9da6;
			padding: 3px 10px;
		}
	}
	.content {
		margin-top: 20px;
		background-color: #ffffff;
		height: 85vh;
		.left {
			border-right: 1px solid #eeeeee;
			height: 85vh;
			min-width: 300px;
			.add {
				background-color: #f8f8f8;
				padding: 10px 0;
				font-size: 24px;
				color: #8d9da6;
				cursor: pointer;
			}
			.add:hover {
				color: #007aff;
			}
			.building-list {
				padding: 25px;
				height: 75vh;
				overflow-y: auto;
				.item {
					padding: 0 15px;
					height: 40px;
					line-height: 40px;
					margin-top: 15px;
					background-color: #f8f8f8;
					.el-icon-caret-right,
					.el-icon-caret-bottom {
						color: #a4a4a4;
						margin-right: 5px;
						font-size: 16px;
					}
					.el-checkbox {
						display: block;
					}
					.btn {
						i {
							background-color: #e8eaec;
							color: #a4a4a4;
							width: 25px;
							height: 25px;
							margin-left: 10px;
							border-radius: 50%;
						}
						i:hover {
							color: red;
						}
					}
				}
				.active {
					background-color: #e4f1ff;
					color: #007aff;
				}
				.floor-list {
					background-color: #f8f8f8;
					border-top: 1px solid #eeeeee;
					padding: 0 15px 15px 30px;
					.floor-item {
						background-color: #f1f1f1;
						padding: 0 15px 0 25px;
						color: #4c5559;
						height: 40px;
						line-height: 40px;
						margin-top: 15px;
						.btn {
							i {
								background-color: #ffffff;
								color: #a4a4a4;
								width: 25px;
								height: 25px;
								margin-left: 10px;
								border-radius: 50%;
							}
							i:hover {
								color: red;
							}
						}
					}
					.active {
						background-color: #e4f1ff;
						color: #007aff;
					}
				}
			}
		}
		.right {
			height: 78vh;
			width: 100%;
			padding: 25px 50px 0 50px;
			.title {
				padding: 0 0 15px 0;
			}
		}
	}

	.el-table th > .cell,
	.el-table .cell {
		overflow: hidden; /*超出部分隐藏*/
		white-space: nowrap; /*不换行*/
		text-overflow: ellipsis; /*超出部分文字以...显示*/
	}
	.el-table thead {
		background: #fafafa !important;
	}
	.el-table th {
		padding: 15px 5px !important;
		background: #fafafa !important;
	}
	.el-table::before {
		left: 0;
		bottom: 0;
		width: 100%;
		height: 0;
	}

	.el-dialog__header {
		border-bottom: 1px solid #eeeeee;
		padding: 10px 20px;
		.el-dialog__title {
			font-size: 16px;
		}
	}
	.el-dialog__body {
		padding: 0 20px 35px 20px;
		.edit-box {
			.input-box {
				margin-top: 35px;
				padding-left: 15px;
				.input-lebal {
					width: 20%;
					text-align: right;
					color: #aeb5b8;
					margin-right: 10px;
					span {
						color: #ff7a65;
						margin-right: 3px;
					}
				}
				.el-icon-remove-outline {
					font-size: 1.5625vw;
					color: #d7d9db;
					margin-left: 10px;
				}
			}
			.upload-file {
				position: absolute;
				left: 0;
				right: 0;
				top: 0;
				bottom: 0;
				opacity: 0;
				cursor: pointer;
			}
			.avatar-uploader .el-upload {
				border: 1px dashed #d9d9d9;
				border-radius: 6px;
				cursor: pointer;
				position: relative;
				overflow: hidden;
			}
			.avatar-uploader .el-upload:hover {
				border-color: #409eff;
			}
			.avatar-uploader-icon {
				font-size: 28px;
				color: #8c939d;
				width: 90px;
				height: 90px;
				line-height: 90px;
				text-align: center;
			}
			.avatar {
				width: 90px;
				height: 90px;
				display: block;
			}
		}
	}
	.el-dialog__footer {
		padding: 15px 20px 15px;
		box-shadow: 0px -5px 10px -5px #eeeeee;
	}
}
</style>
